package leaf.science.ui.rightmenu
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	public class RightMenuBar extends RightMenuBarSkin
	{
		private var _btns:Array;
		private var _nextText:String;
		private var _isClearingText:Boolean;
		
		public function RightMenuBar()
		{
			super();
			setup();
		}
		
		public function set text (value:String):void
		{
//			field_txt.text = value;
			
			_nextText = value;
			if (_nextText.length > 27) _nextText = _nextText.substr(0,24) + "...";
			_isClearingText = true;
			textTransitionStep();
		}
		
		public function get text ():String
		{
			return field_txt.text;
		}
		
		private function setup():void
		{
			field_txt.mouseEnabled = false;
			_btns = [btnUp_mc, btnDn_mc, main_btn];
			for each (var btn:Sprite in _btns){
				btn.addEventListener(MouseEvent.CLICK, onBtnClick);
			}
		}
		
		private function textTransitionStep ():void
		{
			if (_isClearingText){
				field_txt.text = field_txt.text.substr(1);
				if (field_txt.text.length == 0){
					_isClearingText = false;
				}
				setTimeout(textTransitionStep, 5);
			} else {
				field_txt.appendText(_nextText.charAt(field_txt.length));
				if (field_txt.text.length < _nextText.length)
				setTimeout(textTransitionStep, 10);
			}
		}
		
		private function onBtnClick (evt:MouseEvent):void
		{
			switch (evt.target){
				case btnUp_mc :
				dispatchEvent(new Event('up'));
				break;
				
				case btnDn_mc :
				dispatchEvent(new Event('down'));
				break;
				
				case main_btn :
				dispatchEvent(new Event(Event.SELECT));
				break;
			}
		}
		
	}
}